package com.sea.human.controller;

import com.codecrane.core.power.entity.PowerGroup;
import com.codecrane.core.power.service.PowerGroupService;
import com.codecrane.core.power.service.PowerUserService;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.human.entity.PerformanceDetail;
import com.sea.human.entity.PerformanceTask;
import com.sea.human.service.PerformanceDetailService;
import com.sea.human.service.PerformanceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 *  控制器
 * @author 陈鸿飞
 *  <br/>
 *  <br/>
 */
@Controller
@RequestMapping("/sea/human/performanceTask")
public class PerformanceTaskController {

    @Autowired
    private PerformanceTaskService performanceTaskService;
    @Autowired
    private PowerGroupService powerGroupService;
    @Autowired
    private PowerUserService powerUserService;

    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        return "/sea/human/performanceTask/main";
    }

    @RequestMapping("/addPage")
    public String addPage(Model model) {
        return "/sea/human/performanceTask/add";
    }

    @RequestMapping("/addCulture")
    public String addCulture(Model model) {
        List<PowerGroup> powerGroup = powerGroupService.findByPid(3L);
        model.addAttribute("powerGroup",powerGroup);
        return "/sea/human/performanceTask/addCulture";
    }
    @RequestMapping("/editPage")
    public String editPage(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("performanceTask", performanceTaskService.findById(id));
        return "/sea/human/performanceTask/edit";
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     PerformanceTask performanceTask) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<PerformanceTask> performanceList = performanceTaskService.findByCnd(performanceTask);
        PageInfo<PerformanceTask> page = new PageInfo<>(performanceList);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }


    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(PerformanceTask performanceTask) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{

            if (null !=performanceTask && performanceTaskService.save(performanceTask)>0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(PerformanceTask performanceTask) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{

            if (null !=performanceTask && performanceTaskService.usave(performanceTask)>0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxReturn delete(Integer id) {
        AjaxReturn result = Ajax.fail().setMsg("删除失败！");
        try{
            if (null !=id && performanceTaskService.delete(id)>0) {
                result.setOk(true).setMsg("删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
}
